
package business.ordersubsystem;

import static business.util.StringParse.makeString;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;

import middleware.DbConfigProperties;
import middleware.dataaccess.DataAccessSubsystemFacade;
import middleware.exceptions.DatabaseException;
import middleware.externalinterfaces.DataAccessSubsystem;
import middleware.externalinterfaces.DbClass;
import middleware.externalinterfaces.DbConfigKey;
import business.externalinterfaces.Address;
import business.externalinterfaces.CartItem;
import business.externalinterfaces.CreditCard;
import business.externalinterfaces.CustomerProfile;
import business.externalinterfaces.Order;
import business.externalinterfaces.OrderItem;
import business.externalinterfaces.ShoppingCart;
import business.util.OrderUtil;


class DbClassOrder implements DbClass {
	private static final Logger LOG = 
		Logger.getLogger(DbClassOrder.class.getPackage().getName());
	private DataAccessSubsystem dataAccessSS = 
    	new DataAccessSubsystemFacade();
    private String query;
    private String queryType;
    private final String GET_ORDER_ITEMS = "GetOrderItems";
    private final String GET_ORDER_IDS = "GetOrderIds";
    private final String GET_ORDER_DATA = "GetOrderData";	
    private CustomerProfile custProfile;
    private Integer orderId;
    private List<Integer> orderIds;
    private List<OrderItem> orderItems;
    private OrderImpl orderData;
	private OrderItem orderItem;
    private Order order;  
    
    DbClassOrder(){}
    
    DbClassOrder(OrderImpl order){
        this.order = order;
    }
    
    DbClassOrder(OrderItem orderItem ){
        this.orderItem = orderItem;
    }
    
    DbClassOrder(CustomerProfile custProfile) {
    	this.custProfile = custProfile;
    }
    
    DbClassOrder(OrderImpl order, CustomerProfile custProfile){
        this(order);
        this.custProfile = custProfile;
    } 
    
    
    
    OrderImpl getOrderData(Integer orderId) throws DatabaseException {
    	queryType = GET_ORDER_DATA;
    	this.orderId=orderId;  
    	dataAccessSS.atomicRead(this);      	
        return orderData;
    }
    
    // Precondition: CustomerProfile has been set by the constructor
    void submitOrder(ShoppingCart shopCart) {// throws DatabaseException {
	    
	    
    }
    
   
    
    public void buildQuery() {
        if(queryType.equals(GET_ORDER_DATA)) {
        	buildGetOrderDataQuery();
        }
        
    }
    
	

    private void buildGetOrderDataQuery() {
        query = "SELECT orderdate, totalpriceamount FROM Ord WHERE orderid = " + orderId;     
    }
    
    
    
    
    
    
    
    
    
    private void populateOrderData(ResultSet resultSet) throws DatabaseException {  	
        try {
            if(resultSet.next()){
            	orderData=new OrderImpl(orderId, resultSet.getString("orderdate"),
                		           makeString(resultSet.getDouble("totalpriceamount")));
            }
        }
        catch(SQLException e){
            throw new DatabaseException(e);
        }
    }    
 
    public void populateEntity(ResultSet resultSet) throws DatabaseException {
        if(queryType.equals(GET_ORDER_DATA)){
        	populateOrderData(resultSet);
        }       
    }
    
    public String getDbUrl() {
    	DbConfigProperties props = new DbConfigProperties();	
    	return props.getProperty(DbConfigKey.ACCOUNT_DB_URL.getVal());
    }
    
    public String getQuery() {
        return query;
    }
     
    public void setOrderId(Integer orderId){
        this.orderId = orderId;       
    }   
}
